# -*- coding=utf-8-*-
"""
@File:memory_selection
@Author:Created by Han X.Y
@Date:on 2021/7/26 19:26 
"""
"""
把二叉树的任何子节点当成二叉树
左、右子节点都是同一二叉树类的不同对象
"""


class BinaryTree:
    def __init__(self, rootObj):
        self.key = rootObj  # 根对象
        self.leftChild = None
        self.rightChild = None

    def insertLeft(self, newNode):
        """
        插入左子树:
           - 没有左子树时，直接将该节点添加进来即可
           - 存在左子节点时，此时需要插入一个节点并将之前的左子节点降一级
        """
        if self.leftChild is None:
            self.leftChild = BinaryTree(newNode)
        else:
            t = BinaryTree(newNode)
            t.leftChild = self.leftChild
            self.leftChild = t

    def insertRight(self, newNode):
        """
        插入右子树
        """
        if self.rightChild is None:
            self.rightChild = BinaryTree(newNode)
        else:
            t = BinaryTree(newNode)
            t.rightChild = self.rightChild
            self.rightChild = t

    def getRightChild(self):
        return self.rightChild

    def getLeftChild(self):
        return self.leftChild

    def getRootVal(self):
        return self.key

    def setRootVal(self, obj):
        self.key = obj

if __name__ == '__main__':
    r=BinaryTree('r')
    print(r.getRootVal())
    r.insertLeft('b')
    r.insertLeft('c')
    print(r.getLeftChild().getRootVal())
    print(r)